package com.jasper.controller.center;

import com.jasper.controller.BaseController;
import com.jasper.enums.YesOrNo;
import com.jasper.pojo.OrderItems;
import com.jasper.pojo.Orders;
import com.jasper.pojo.Users;
import com.jasper.pojo.bo.center.CenterUserBO;
import com.jasper.pojo.bo.center.OrderItemsCommentBO;
import com.jasper.resource.FileUpload;
import com.jasper.service.center.CenterUserService;
import com.jasper.service.center.MyCommentsService;
import com.jasper.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jasper.utils.DateUtils.DATE_PATTERN;

@Api(value = "用户中心评价模块", tags = {"用户中心评价模块"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {
    @Autowired
    private MyCommentsService myCommentsService;
    @Autowired
    private FileUpload fileUpload;

    @ApiOperation(value="订单评论列表", notes = "订单评论列表", httpMethod = "POST")
    @PostMapping("pending")
    public JSONResult pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) {

        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(!checkResult.isOK()){
            return checkResult;
        }

        Orders myOrder = (Orders)checkResult.getData();

        if (myOrder.getIsComment() == YesOrNo.YES.type) {
            JSONResult.errorMsg("该笔订单已经评价");
        }

        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);

        return JSONResult.ok(list);
    }

    @ApiOperation(value="保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("saveList")
    public JSONResult saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList) {

        System.out.println(commentList);

        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(!checkResult.isOK()){
            return checkResult;
        }

        if(CollectionUtils.isEmpty(commentList)){
            return JSONResult.errorMsg("评论内容不能为空");
        }

        myCommentsService.saveComments(userId, orderId, commentList);

        return JSONResult.ok();
    }

    @ApiOperation(value="查询我的评价", notes = "查询我的评价", httpMethod = "POST")
    @PostMapping("query")
    public JSONResult query(@ApiParam(name = "userId", value = "用户Id", required = true)
                            @RequestParam String userId,
                            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
                            @RequestParam Integer page,
                            @ApiParam(name = "pageSize", value = "分页的每一页显示的记录数", required = false)
                            @RequestParam Integer pageSize){

        if(userId==null) return JSONResult.errorMsg(null);

        page = page==null?1:page;
        pageSize =pageSize ==null?10:pageSize;

        PagedGridResult result = myCommentsService.queryMyComments(userId, page, pageSize);

        return JSONResult.ok(result);
    }

}
